package javacommon.springmvc.interceptor;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import cn.org.rapid_framework.web.httpinclude.HttpInclude;
import cn.org.rapid_framework.web.scope.Flash;

/**
 * 
 * @author badqiu
 * restore  global object
 */
public class SharedRenderVariableInterceptor extends HandlerInterceptorAdapter
		implements InitializingBean {
	static Log log = LogFactory.getLog(SharedRenderVariableInterceptor.class);

	
	private Map<String, Object> globalRenderVariables = new HashMap<String, Object>();

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		

		if (modelAndView == null) {
			return;
		}

		String viewName = modelAndView.getViewName();
		if (viewName != null && !viewName.startsWith("redirect:")) {
			modelAndView.addAllObjects(globalRenderVariables);
			modelAndView.addAllObjects(perRequest(request, response));
		}
		
	}

	protected Map<String, Object> perRequest(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, Object> model = new HashMap<String, Object>();
	    model.put("context",request.getContextPath());
		model.put("share_current_request_time", new Date());
		model.put("share_current_login_username", "badqiu");
		model.put("ctx", request.getContextPath());
		model.put("flash", Flash.current().getData());
		model.put("httpInclude", new HttpInclude(request, response));
		return model;
	}

	private void initSharedRenderVariables() {
		globalRenderVariables.put("global_system_start_time", new Date());
		globalRenderVariables.put("url_prefix",
				"http://www.rapid-framework.org.cn");
		globalRenderVariables.put("img_url_prefix",
				"http://img.rapid-framework.org.cn");
		globalRenderVariables.put("media_url_prefix",
				"http://media.rapid-framework.org.cn");
		globalRenderVariables.put("static_url_prefix",
				"http://static.rapid-framework.org.cn");

	}

	
	public void afterPropertiesSet() throws Exception {
		initSharedRenderVariables();
	}
}
